package com.android.graffiti;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

import com.photoeditor.demo.ui.widget.image.edit.BeautyBarView;
import com.photoeditor.demo.ui.widget.image.edit.EditOperationView;
import com.photoeditor.demo.ui.widget.image.edit.OtherBarView;

import static com.photoeditor.demo.ui.widget.image.edit.BeautyBarView.TYPE_BEAUTY;
import static com.photoeditor.demo.ui.widget.image.edit.BeautyBarView.TYPE_BODY;
import static com.photoeditor.demo.ui.widget.image.edit.BeautyBarView.TYPE_NONE;
import static com.photoeditor.demo.ui.widget.image.edit.EditOperationView.TYPE_EDIT;
import static com.photoeditor.demo.ui.widget.image.edit.OtherBarView.TYPE_OTHER_BAR;


/**
 * 带有左右滑动指示器的水平滚动控件
 */
public class IndicativeHorizontalScrollView extends HorizontalScrollView
{
    private Bitmap mLeftIndicator;
    private Bitmap mRightIndicator;
    
    /**
     * 不透明画笔
     */
    private Paint mPaint1;
    
    /**
     * 半透明画笔
     */
    private Paint mPaint2;
    
    private long mLastTime;

    private int mLastScrollX = 0;

    private BeautyBarView mBeautyBarView;

    private EditOperationView mEditOperationView;

    private OtherBarView mOtherBarView;


    private HorizontalScrollChangeListener mHorScrollListener;

    /**
     * 动画间隔
     */
    private final int DURATION = 500;

    private boolean mScrollable = true;
    
    public IndicativeHorizontalScrollView(Context context)
    {
        super(context);
        initilize(context);
    }
    
    public IndicativeHorizontalScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initilize(context);
    }

    public IndicativeHorizontalScrollView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        initilize(context);
    }
    
    
    private void initilize(Context context)
    {
        mLastTime = System.currentTimeMillis();
        
        mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        
        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setAlpha(127);

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mHorScrollListener != null) {
            mHorScrollListener.onScroll(l, oldl);
        }
    }

    public void setOnHorScrollChangeListener(HorizontalScrollChangeListener listener) {
        mHorScrollListener = listener;
    }

    /**
     * 通过资源文件ID来获取图片内容
     * @param id 资源文件ID
     * @param context 上下文对象
     * @return 位图对象，如果读取失败将返回null
     */
    public static Bitmap readImageFileFromResource(int id, Context context)
    {
        if(id == 0)
        {
            return null;
        }

        try
        {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
            if (bitmap == null)
            {
            }
            return bitmap;
        }
        catch (OutOfMemoryError er)
        {
        }
        catch (Exception e)
        {
        }

        return null;
    }

    public void setBeautyBarView(BeautyBarView view) {
        mBeautyBarView = view;
    }

    public void setEditOperationView(EditOperationView editOperationView) {
        mEditOperationView = editOperationView;
    }

    public void setOtherBarView(OtherBarView otherBarView) {
        mOtherBarView = otherBarView;
    }

    public void setScrollable(boolean scrollable) {
        mScrollable = scrollable;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_MOVE && !mScrollable) {
            return false;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.dispatchDraw(canvas);
        
        View childView = getChildAt(0);
        if(childView != null && childView.getWidth() > getWidth())
        {
            int scrollX = getScrollX();
            if(scrollX != 0 && mLeftIndicator != null)
            {
                int left = scrollX + 2;
                int top = (getHeight() - mLeftIndicator.getHeight()) / 2;
                long time = System.currentTimeMillis();
                if(time - mLastTime > DURATION){
                	if(time - mLastTime > 2 * DURATION){
                		mLastTime = time;
                	}
                	canvas.drawBitmap(mLeftIndicator, left, top, mPaint2);
                } else{
                	canvas.drawBitmap(mLeftIndicator, left, top, mPaint1);
                }
            }
            
            int visualRegionX = getWidth() - getPaddingLeft() - getPaddingRight();
            int maxScrollX = childView.getWidth() - visualRegionX;
            if(scrollX != maxScrollX & mRightIndicator != null)
            {
                int left = getWidth() - mRightIndicator.getWidth() + scrollX - 2;
                int top = (getHeight() - mRightIndicator.getHeight()) / 2;
                long time = System.currentTimeMillis();
                if(time - mLastTime > DURATION){
                	if(time - mLastTime > 2 * DURATION){
                		mLastTime = time;
                	}
                	canvas.drawBitmap(mRightIndicator, left, top, mPaint2);
                } else{
                	canvas.drawBitmap(mRightIndicator, left, top, mPaint1);
                }
            }
            if (scrollX != mLastScrollX) {
                if (mOnScrollXListener != null) {
                    mOnScrollXListener.onScrollX(scrollX, scrollX - mLastScrollX >= 0);
                }
                mLastScrollX = scrollX;
            }
            if(mLeftIndicator != null && mRightIndicator != null) {
                postInvalidate();
            }
        }
    }

    private OnScrollXListener mOnScrollXListener;

    public void setOnScrollXListener(OnScrollXListener onScrollXListener) {
        mOnScrollXListener = onScrollXListener;
    }

    private int mType = TYPE_NONE;

    public void setType(int type) {
        mType = type;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mType == TYPE_BEAUTY || mType == TYPE_BODY) {
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                postDelayed(mStatisticTask, 400);
                return super.onTouchEvent(ev);
            }
            return super.onTouchEvent(ev);
        } else if (mType == TYPE_EDIT) {
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                postDelayed(mEditStatisticTask, 400);
                return super.onTouchEvent(ev);
            }
        } else if (mType == TYPE_OTHER_BAR) {
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                postDelayed(mOtherBarViewStatisticTask, 400);
                return super.onTouchEvent(ev);
            }
        }
        return super.onTouchEvent(ev);
    }

    private Runnable mStatisticTask = new Runnable() {
        @Override
        public void run() {
            try {
                mBeautyBarView.statisticItemShow(getScrollX());
            } catch (Exception e) {
                e.printStackTrace();
                if (mStatisticTask != null) {
                    removeCallbacks(mStatisticTask);
                }
            }
        }
    };

    public void cancelStatisticTask() {
        if (mStatisticTask != null) {
            removeCallbacks(mStatisticTask);
        }
    }

    private Runnable mEditStatisticTask = new Runnable() {
        @Override
        public void run() {
            try {
                mEditOperationView.statisticItemShow(getScrollX());
            } catch (Exception e) {
                e.printStackTrace();
                if (mEditStatisticTask != null) {
                    removeCallbacks(mEditStatisticTask);
                }
            }
        }
    };

    public void cancelEditStatisticTask() {
        if (mEditStatisticTask != null) {
            removeCallbacks(mEditStatisticTask);
        }
    }

    private Runnable mOtherBarViewStatisticTask = new Runnable() {
        @Override
        public void run() {
            try {
                mOtherBarView.statisticItemShow(getScrollX());
            } catch (Exception e) {
                e.printStackTrace();
                if (mOtherBarViewStatisticTask != null) {
                    removeCallbacks(mOtherBarViewStatisticTask);
                }
            }
        }
    };

    public void cancelOtherBarViewStatisticTask() {
        if (mOtherBarViewStatisticTask != null) {
            removeCallbacks(mOtherBarViewStatisticTask);
        }
    }

    private Runnable mCollageBottomBarViewStatisticTask = new Runnable() {
        @Override
        public void run() {
            try {
//                mCollageActivity.statisticItemShow(getScrollX());
            } catch (Exception e) {
                e.printStackTrace();
                if (mCollageBottomBarViewStatisticTask != null) {
                    removeCallbacks(mCollageBottomBarViewStatisticTask);
                }
            }
        }
    };

    public void cancelCollageBottomBarViewStatisticTask() {
        if (mCollageBottomBarViewStatisticTask != null) {
            removeCallbacks(mCollageBottomBarViewStatisticTask);
        }
    }

    public interface OnScrollXListener {
        /**
         *
         * @param scrollX 左边缘到原来的距离
         * @param toLeft
         */
        void onScrollX(int scrollX, boolean toLeft);
    }

    public interface HorizontalScrollChangeListener {
        void onScroll(int l, int oldl);
    }

}
